$(function() {
	new Vue({
		el: "#box",
		data: {
			tabIndex: 0,
			checked: "",
			username: "",
			passwd: "",
			age: 0,
			addr: "",
			company: "",
			tel: "",
			email: "",
			gender: 1,
			loginTip: ""

		},
		created: function() {
			if (this.getCookie("memoryFlag") == "true") {
				this.username = this.getCookie("username");
				this.passwd = this.getCookie("passwd");
				this.checked = this.getCookie("memoryFlag");
			}
		},
		methods: {
			setCookie: function(cname, cvalue, exdays) {
				var d = new Date();
				d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
				var expires = "expires=" + d.toUTCString();
				console.info(cname + "=" + cvalue + "; " + expires);
				document.cookie = cname + "=" + cvalue + "; " + expires;
				// console.info(document.cookie);
			},
			//获取cookie
			getCookie: function(cname) {
				var name = cname + "=";
				var ca = document.cookie.split(';');
				for (var i = 0; i < ca.length; i++) {
					var c = ca[i];
					while (c.charAt(0) == ' ') c = c.substring(1);
					if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
				}
				return "";
			},
			//清除cookie
			clearCookie: function() {
				this.setCookie("username", "", -1);
			},
			checkCookie: function() {
				var user = this.getCookie("username");
				if (user != "") {
					alert("Welcome again " + user);
				} else {
					user = prompt("Please enter your name:", "");
					if (user != "" && user != null) {
						this.setCookie("username", user, 365);
					}
				}
			},
			login: function(event) {
				var that = this;
				if (this.username.trim() == "" || this.username == undefined) {
					// alert("用户名不能为空");
					this.loginTip = "用户名不能为空";
				} else if (this.passwd.trim() == "" || this.passwd == undefined) {
					// alert("密码不能为空");
					this.loginTip = "密码不能为空";
				} else {
					$.ajax({
						url: "http://127.0.0.1:8080/homework/user/login",
						method: "post",
						dataType: "json",
						data: {
							"username": this.username,
							"passwd": this.passwd
						},
						success: function(res) {
							console.log("res: ", res);
							var url = document.referrer;
							if (res.code == "200") {
								if (that.checked) {
									setCookie("memoryFlag", true, 15, "/vue/");
								} else {
									setCookie("memoryFlag", false, 15, "/vue/");
								}
								setCookie("logined", true, 15, "/vue/");
								setCookie("username", that.username, 15, "/vue/");
								setCookie("passwd", that.passwd, 15, "/vue/");
								if (url == "" || url == undefined) {
									location.href = "../index.html";
								} else {
									location.href = url;
								}
							} else {
								that.loginTip = res.msg;
							}
						},
						error: function(error) {
							console.log("error: ", error);
						}
					});
				}
			},
			register: function(res) {
				console.log("res: ", this.username);
				var sex = "";
				var that = this;
				if (this.gender == 1) {
					sex = "男";
				} else {
					sex = "女";
				}
				$.ajax({
					url: "http://127.0.0.1:8080/homework/user/queryById",
					method: "post",
					dataType: "json",
					data: {
						username: this.username
					},
					success: function(res) {
						console.log("res: ", res);
						if (res.data.length == 0) {
							$.ajax({
								url: "http://127.0.0.1:8080/homework/user/register",
								method: "post",
								dataType: "json",
								data: {
									username: that.username,
									passwd: that.passwd,
									age: that.age,
									gender: that.gender,
									addr: that.addr,
									company: that.company,
									email: that.email,
									tel: that.tel
								},
								success: function(res) {
									console.log("res: ", res);

								},
								error: function(error) {
									console.log("error: ", error);
								}

							});
						} else {
							alert("用户已[" + that.username + "]存在");
						}
					},
					error: function(error) {
						console.log("error: ", error);
					}
				});

			},
			reset: function() {},
			loginTab: function() {
				this.tabIndex = 0;
				$(window).resize();
			},
			registerTab: function() {
				this.tabIndex = 1;
				$(window).resize();
			}
		}
	});
})